package com.icss.ah.clerkAccount.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.util.MD5Util;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.login.bean.UserInfoBean;
/**
 * 店员维护Action
 * @author wangjun
 *
 */
@Controller
@Scope("prototype")
public class ClerkAccountAction extends BaseAction {
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	private String operat;			//操作标识
	private String personAccount;	//店员帐号
	private String personName;		//店员姓名
	private String loginPwd;		//登录密码
	private String mobile;			//电话号码
	private String email;			//邮箱帐号
	private String isKf;			//是否客服
	private String personUuid;		//店员id
	
	/**
	 * 进入店员列表界面
	 * @return
	 */
	public String toClerkAccountInfo(){
		request = this.getRequest();
		return "toClerkAccountInfo";
	}
	/**
	 * 查询店员列表
	 */
	public void queryClerkAccountList(){
		request = this.getRequest();
		String result = "";
		try {
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();   //获取店铺id 
			String personName = request.getParameter("personName");//店员姓名（查询条件）
			personName = personName == null ? "" : personName;
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("shopId",shopId);
			map.put("personName",personName);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> clerkAccountList = new ArrayList();
			clerkAccountList = clerkAccountService.queryClerkAccountList(map);	
			long clerkAccountCount = clerkAccountService.queryClerkAccountCount(map);//查询总记录数
			dataGridJson.setRows(clerkAccountList);
			dataGridJson.setTotal(clerkAccountCount); 
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("queryClerkAccountList|查询店员列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("queryClerkAccountList|查询店员列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	
	/**
	 * 修改店员
	 * @return
	 */
	public String editClerk(){
		request = this.getRequest();
		String personUuid = request.getParameter("personUuid");
		personUuid = personUuid == null ? "" : personUuid;
		HashMap map = new HashMap();
		map.put("operat", operat);
		Map clerkMap = new HashMap();
		if("edit".equals(operat)){
			map.put("personUuid", personUuid);
			clerkMap = clerkAccountService.queryClerkMap(map);
		}
		request.setAttribute("clerkMap", clerkMap);
		request.setAttribute("operat", operat);
		return "toClerkEdit";
	}
	
	/**
	 * 保存店员信息操作(添加或修改)
	 * @return
	 */
	public String saveClerk() {
		request = this.getRequest();
		jsonText = new Json();
		try {
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();   //获取店铺id
			HashMap map = new HashMap();
			map.put("personAccount", personAccount);
			map.put("personName",personName );
			map.put("loginPwd",MD5Util.toMD5(loginPwd));
			map.put("mobile", mobile);
			map.put("email",email );
			map.put("isKf", isKf);
			map.put("shopId", shopId);
			boolean revl = false;
			revl =  clerkAccountService.saveClerk(map,personUuid,operat);
			if(revl){
				jsonText.setMsg("保存成功！");
				jsonText.setSuccess(true);
			}else{
				jsonText.setMsg("保存失败！");
				jsonText.setSuccess(true);
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return "saveClerk";
	}
	
	/**
	 * 重置密码
	 * @return
	 */
	public String resetPassword(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String personUuid = request.getParameter("personUuid");
			personUuid = personUuid == null ? "" : personUuid;
			String password = ResourceLoader.getInstance("icss").getString("rePassword");
			HashMap map = new HashMap();
			map.put("personUuid", personUuid);
			map.put("password",MD5Util.toMD5(password));
			boolean revl = false;
			revl = clerkAccountService.resetPassword(map);
			jsonText.setSuccess(revl);
			if(revl){
				jsonText.setMsg(password);
			} else {
				jsonText.setMsg("重置密码失败");
			}
		} catch (Exception e) {
			jsonText.setMsg(e.getMessage());
			jsonText.setSuccess(false);
			errlog.error("ClerkAccountAction|resetPassword|重置店铺密码失败：",e);
		}
		return "jsonText";
	}
	/**
	 * 删除店员
	 * @return
	 */
	public String deleteClerk(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String personUuid = request.getParameter("personUuid");
			personUuid = personUuid == null ? "" : personUuid;
			HashMap map = new HashMap();
			map.put("personUuid", personUuid);
			boolean revl = false;
			revl = clerkAccountService.deleteClerk(map);
			jsonText.setSuccess(true);
			jsonText.setMsg("删除成功！");
		} catch (Exception e) {
			jsonText.setMsg("删除失败！");
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	/**
	 * 停用启用操作
	 * @return
	 */
	public String dealStatus(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String personUuid = request.getParameter("personUuid");//人员id
			personUuid = personUuid == null ? "" : personUuid;
			String status = request.getParameter("status");//停用或启用状态
			status = status == null ? "" : status;
			HashMap map = new HashMap();
			map.put("personUuid", personUuid);
			map.put("status", status);
			boolean revl = false;
			revl = clerkAccountService.dealStatus(map);
			jsonText.setSuccess(true);
			jsonText.setMsg("操作成功！");
		} catch (Exception e) {
			jsonText.setMsg("操作失败！");
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	/**
	 * 验证账号是否存在
	 * @return
	 */
	public String checkUserAccountExist(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			HashMap map = new HashMap();
			map.put("personAccount", personAccount);
			boolean revl = false;
			revl = clerkAccountService.checkUserAccountExist(map);
			jsonText.setSuccess(revl);
			if(revl){
				jsonText.setMsg("验证通过");
			} else {
				jsonText.setMsg("账号已存在");
			}
		} catch (Exception e) {
			jsonText.setMsg("验证失败");
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	public String getOperat() {
		return operat;
	}

	public void setOperat(String operat) {
		this.operat = operat;
	}

	public String getPersonAccount() {
		return personAccount;
	}

	public void setPersonAccount(String personAccount) {
		this.personAccount = personAccount;
	}

	public String getPersonName() {
		return personName;
	}

	public void setPersonName(String personName) {
		this.personName = personName;
	}

	public String getLoginPwd() {
		return loginPwd;
	}

	public void setLoginPwd(String loginPwd) {
		this.loginPwd = loginPwd;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getIsKf() {
		return isKf;
	}

	public void setIsKf(String isKf) {
		this.isKf = isKf;
	}

	public String getPersonUuid() {
		return personUuid;
	}

	public void setPersonUuid(String personUuid) {
		this.personUuid = personUuid;
	}
	
}
